package com.ssps.back.manage.user.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssps.back.manage.user.service.Manage_UserService;
import com.ssps.entity.Role;
import com.ssps.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manage")
public class Manage_UserController {
    @Autowired
    Manage_UserService manage_userService;

    @RequestMapping("index")
    @ResponseBody
    public Map<String, Object> UserIndex(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         String condition, String rname) {
        Map<String, Object> map = new HashMap<>();

        try {
            PageHelper.startPage(pageNum, pageSize);
            List<UserInfo> list = manage_userService.selectByCondition(condition, rname);
            PageInfo<UserInfo> pageinfo = new PageInfo<>(list);
            map.put("userList", list);
            map.put("pageInfo", pageinfo);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    @RequestMapping("roleIndex")
    @ResponseBody
    public Map<String,Object> roleIndex(Integer uid){
        Map<String,Object> map = new HashMap<>();
        try {
            List<Role>unAssignRole = manage_userService.selectUnassignRole(uid);
            List<Role>AssignRole = manage_userService.selectassignRole(uid);
            map.put("unassignRoleList",unAssignRole);
            map.put("assignRoleList",AssignRole);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    @RequestMapping("giveRole")
    @ResponseBody
    public Map<String,Object> giveRole(Integer uid,Integer[] rids){
        Map<String,Object> map = new HashMap<>();
        try {
            manage_userService.add(uid,rids);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    @RequestMapping("recycleRole")
    @ResponseBody
    public Map<String,Object> recycleRole(Integer uid,Integer[] rids2){
        Map<String,Object> map = new HashMap<>();
        try {
            manage_userService.delete(uid,rids2);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }

    @RequestMapping("assignRole")
    public String assign(Integer uid, Model model){
        System.out.println(uid);
        model.addAttribute("uid", uid);
        return "/manageWeb/assignRole";
    }
@RequestMapping("userindex")
    public String userindex(){
        return "/manageWeb/UserList";
}

}
